﻿
using BNB7.Entry.Items;
using Microsoft.Xna.Framework;
using System.IO;
using System.Xml.Serialization;
using BNB7.Entry.IniData;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Content;
namespace BNB7.Entry
{
    #region --- 角色相关 ---

    /// <summary>
    /// 状态类定义
    /// </summary>
    public enum RoleState
    {
        rsStart,
        rsNormal,
        rsIdle,
        rsTraped,
        rsOnTrap,
        rsOnSave,
        rsOnDie,
        rsDead,
        rsWin,
        rsOnRide,
        rsOnDismount
    }

    /// <summary>
    /// 方向
    /// </summary>
    public enum Direction
    {
        drUp,
        drDown,
        drLeft,
        drRight,
        drMiddle,
        drUpLeft,
        drUpRight,
        drDownLeft,
        drDownRight
    };

    /// <summary>
    /// 座骑
    /// </summary>
    public enum VehicleType
    {
        vtNone,
        vtSlowTurtle,
        vtFastTurtle,
        vtOwl,
        vtSlowUFO,
        vtFastUFO
    };

    /// <summary>
    /// 能力定义
    /// </summary>
    public class Ability
    {
        public int nPopoPower;
        public int nPopoMax;
        public int nUsedPopo;
        public int nSpeed;
        public bool bKick;
        public VehicleType Vehicle;
    };

    #endregion

    #region --- 地图相关 ---

    public enum ItemType
    {
	    itNone,
	    itFirm,
	    itSolid,
	    itBox,
	    itPopo,
	    itGift
    };

    public enum BlindageType
    {
	    btNone,
	    btBlind,
	    btBush,
	    btTent
    };

    /// <summary>
    /// 块爆炸数据
    /// </summary>
    public class ExplosionData
    {
        public Direction Dr;	// 方向	
        public bool IsHead;	    // 是否为边界
        public byte Gift;	    // 是否有宝物
        public byte Timer;
        private byte _Status;
        public byte Status
        {
            get
            {
                return _Status;
            }
            set
            {
                _Status = value;
            }
        }
        public ushort Dead;
    };

    public class Cell
    {
        public int Ground;
        public ItemType Type;
        public BlindageType Blind;
        public int ID;

        public ItemBase CurItem
        {
            get;
            set;
        }

        public Blindage CurBlindage;
        public ExplosionData CurExplosion
        {
            get;
            set;
        }

        public Cell()
        {
            CurExplosion = new ExplosionData();
        }

        public Cell Clone()
        {
            return MemberwiseClone() as Cell;
        }
    };

    #endregion

    public static class Common
    {
        public const int MAX_SPEED = 7;                                    // 最快速度
        public const int MAX_POWER = 7;                                    // 泡泡最强能力
        public static int[] VEHICLE_SPEED = { 0, 1, 7, 4, 1, 7 };           // 座骑相应的速度

        public const int CELL_SIZE = 40;
        public const int CELL_X = 15;
        public const int CELL_Y = 13;
        public const int CELL_X_ALL = 17;
        public const int CELL_Y_ALL = 15;
        public const int CELL_NUM = CELL_X_ALL * CELL_Y_ALL;
        public const int SCENE_LEFT = 20;
        public const int SCENE_TOP = 41;

        public static readonly int[] seq = new int[4] { 0, 1, 0, 2 };

        private const string IniRootDir = "Ini";
        private const string FixedItemPath = "Ini\\FixedItem.dat";
        private const string RoleSharedPath = "Ini\\RoleShared.dat";

        private const string MapTypeDir = "Ini\\MapType";
        private const string RoleDir = "Ini\\Role";

        public static void MoveAhead(int nOldX, int nOldY, ref int pnNewX, ref int pnNewY, Direction dir)
        {
	        switch (dir)
	        {
                case Direction.drUp:
		            pnNewY = nOldY - 1;
		            pnNewX = nOldX;
		            break;
                case Direction.drDown:
		            pnNewY = nOldY + 1;
		            pnNewX = nOldX;
		            break;
                case Direction.drLeft:
		            pnNewX = nOldX - 1;
		            pnNewY = nOldY;
		            break;
                case Direction.drRight:
		            pnNewX = nOldX + 1;
		            pnNewY = nOldY;
                    break;
	        }
        }

        public static void LoadIniFile(out FixedItemIni fixedItemIni, out RoleSharedIni roleSharedIni,
            out List<MapTypeIni> mapList, out List<RoleIni> roleList, out List<string> mapDataList)
        {
            fixedItemIni = null;
            roleSharedIni = null;
            mapDataList = new List<string>();
            mapList = new List<MapTypeIni>();
            roleList = new List<RoleIni>();

            Dictionary<string, List<string>> dicFiles = new Dictionary<string, List<string>>();

            //#if WINDOWS_PHONE 

            using (Stream stream = Microsoft.Xna.Framework.TitleContainer.OpenStream("Map.lst"))
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<string>));
                mapDataList = (List<string>)xmlSerializer.Deserialize(stream);
            }

            using (Stream stream = Microsoft.Xna.Framework.TitleContainer.OpenStream("Ini.lst"))
            {
                using (BinaryReader reader = new BinaryReader(stream))
                {
                    while (reader.BaseStream.Position < reader.BaseStream.Length)
                    {
                        string line = reader.ReadString();

                        // if it isn't blank, we can create a screen from it
                        if (!string.IsNullOrEmpty(line))
                        {
                            string[] str = line.Split('$');
                            if (str.Length == 2)
                            {
                                if(dicFiles.ContainsKey(str[0]))
                                {
                                    dicFiles[str[0]].Add(str[1]);
                                }
                                else
                                {
                                    List<string> ls = new List<string>();
                                    ls.Add(str[1]);
                                    dicFiles.Add(str[0], ls);
                                }
                            }
                        }
                    }
                }
            }

            foreach (string strKey in dicFiles.Keys)
            {
                switch (strKey)
                {
                    case "FixedItemIni":
                        {
                            using (Stream stream = Microsoft.Xna.Framework.TitleContainer.OpenStream(dicFiles[strKey][0]))
                            {
                                XmlSerializer xmlSerializer = new XmlSerializer(typeof(FixedItemIni));
                                fixedItemIni = (FixedItemIni)xmlSerializer.Deserialize(stream);
                            }
                        }
                        break;
                    case "RoleSharedIni":
                        {
                            using (Stream stream = Microsoft.Xna.Framework.TitleContainer.OpenStream(dicFiles[strKey][0]))
                            {
                                XmlSerializer xmlSerializer = new XmlSerializer(typeof(RoleSharedIni));
                                roleSharedIni = (RoleSharedIni)xmlSerializer.Deserialize(stream);
                            }
                        }
                        break;
                    case "MapTypeIni":
                        {
                            foreach (string path in dicFiles[strKey])
                            {
                                using (Stream stream = Microsoft.Xna.Framework.TitleContainer.OpenStream(path))
                                {
                                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(MapTypeIni));
                                    MapTypeIni mapTypeIni = (MapTypeIni)xmlSerializer.Deserialize(stream);
                                    mapList.Add(mapTypeIni);
                                }
                            }
                        }
                        break;
                    case "RoleIni":
                        {
                            foreach (string path in dicFiles[strKey])
                            {
                                using (Stream stream = Microsoft.Xna.Framework.TitleContainer.OpenStream(path))
                                {
                                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(RoleIni));
                                    RoleIni role = (RoleIni)xmlSerializer.Deserialize(stream);
                                    roleList.Add(role);
                                }
                            }
                        }
                        break;
                }
            }


            //using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
            //{
            //    if (storage.DirectoryExists(IniRootDir))
            //    {
            //        // FixedItem.data
            //        if (storage.FileExists(FixedItemPath))
            //        {
            //            using (IsolatedStorageFileStream stream = storage.OpenFile(FixedItemPath, FileMode.Open, FileAccess.Read))
            //            {
            //                XmlSerializer xmlSerializer = new XmlSerializer(typeof(FixedItemIni));
            //                fixedItemIni = (FixedItemIni)xmlSerializer.Deserialize(stream); 
            //            }
            //        }

            //        // RoleShared.data
            //        if (storage.FileExists(RoleSharedPath))
            //        {
            //            using (IsolatedStorageFileStream stream = storage.OpenFile(RoleSharedPath, FileMode.Open, FileAccess.Read))
            //            {
            //                XmlSerializer xmlSerializer = new XmlSerializer(typeof(RoleSharedIni));
            //                roleSharedIni = (RoleSharedIni)xmlSerializer.Deserialize(stream);
            //            }
            //        }

            //        #region --- 地图列表 ---

            //        if (storage.DirectoryExists(MapTypeDir))
            //        {
            //            string[] mapfiles = storage.GetFileNames(MapTypeDir);
            //            foreach (string mapfile in mapfiles)
            //            {
            //                using (IsolatedStorageFileStream stream = storage.OpenFile(mapfile, FileMode.Open, FileAccess.Read))
            //                {
            //                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(MapTypeIni));
            //                    MapTypeIni mapTypeIni = (MapTypeIni)xmlSerializer.Deserialize(stream);
            //                    mapList.Add(mapTypeIni);
            //                }
            //            }
            //        }

            //        #endregion

            //        #region --- 角色列表 ---

            //        if (storage.DirectoryExists(RoleDir))
            //        {
            //            string[] rolefiles = storage.GetFileNames(RoleDir);
            //            foreach (string rolePath in rolefiles)
            //            {
            //                using (IsolatedStorageFileStream stream = storage.OpenFile(rolePath, FileMode.Open, FileAccess.Read))
            //                {
            //                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(RoleIni));
            //                    RoleIni role = (RoleIni)xmlSerializer.Deserialize(stream);
            //                    roleList.Add(role);
            //                }
            //            }
            //        }

            //        #endregion
            //    }
            //}

//#endif

        }
    }
}